// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import java.util.Arrays;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.dengues.designer.ui.properties.cmd.PropertyChangeTextCommand;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-1 下午02:31:13
 */
public class ComboPropertyController extends AbstractPropertyController {

    /**
     * Qiang.Zhang.Adolf@gmail.com ComboPropertyController constructor comment.
     * 
     * @param dps
     */
    public ComboPropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.property.IPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(final Composite composite, final NodePropertyParameter param, Control lastControl) {
        Composite subcomposite = (Composite) super.createControl(composite, param, lastControl);
        CLabel cLabel = getWidgetFactory().createCLabel(subcomposite, param.getDisplayName());
        GridData gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL;
        cLabel.setLayoutData(gridData);
        CCombo combo = getWidgetFactory().createCCombo(subcomposite, SWT.BORDER);
        ControlDecoration controlDecoration = new ControlDecoration(combo, SWT.RIGHT | SWT.TOP);
        if (param.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        combo = (CCombo) controlDecoration.getControl();
        GridData data = new GridData();
        data.heightHint = STANDARD_HEIGHT;
        data.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
        combo.setLayoutData(data);
        combo.setData(PARAMETER_NAME, param.getName());
        final String[] displayItems = getDisplayItems(param);
        final String[] valueItems = getValueItems(param);
        combo.setItems(displayItems);
        if (param.getValue() instanceof String && !"".equals(param.getValue())) { //$NON-NLS-1$
            String value = (String) param.getValue();
            int indexOf = Arrays.asList(valueItems).indexOf(value);
            if (indexOf == -1) {
                indexOf = 0;
            }
            value = displayItems[indexOf];
            combo.setText(value);
        } else if (displayItems.length > 0) {
            combo.select(0);
            PropertyChangeTextCommand command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), valueItems[0]);
            getCommandStack().execute(command);
        }
        paramName = param.getName();
        combo.setBackground(composite.getBackground());
        combo.setEditable(false);
        combo.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                CCombo combo1 = (CCombo) e.getSource();
                PropertyChangeTextCommand command = new PropertyChangeTextCommand(dps.getElem(), param.getName(),
                        valueItems[combo1.getSelectionIndex()]);
                getCommandStack().execute(command);
                paramName = param.getName();
                updateComponentVisiable();
            }

        });
        curControlMap.put(param.getName(), combo);
        return combo;
    }

}
